<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta http-equiv="expires" content="no-cache">
<title>Insert title here</title>
<%
	//防止浏览器产生缓存
	response.setHeader("Pragma","No-Cache"); 
	response.setHeader("Cache-Control","No-Cache"); 
	response.setDateHeader("Expires", 0); 

	//定义基础路径，从DispatcherServlet获取数据
	String path = request.getContextPath() + "/";	
	String resourcesPath = path + "resources/";
	String omPath = resourcesPath + "operamasks-ui-2.0/";		
	
%>
<link href="<%=omPath%>css/default/om-default.css" rel="stylesheet"	type="text/css" />
<script src="<%=omPath%>js/jquery.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/jquery.xml2json.js" type="text/javascript"></script>
<script src="<%=omPath%>js/operamasks-ui.min.js" type="text/javascript"></script>
<script src="<%=path%>resources/sm/js/om-dialog-url.js" type="text/javascript"></script>
<style type="text/css">
	.search-button{
	padding-left: 50px;
	}
   	.myinput input{
    border:1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput2 input{
    border:0.1px solid;
    border-color:lightblue;
    height: 18px;
    width : 160px;
   	}
   	.myinput input:focus{border: 1px solid #3A76C2;}
   	.myinput2 input:focus{border: 0.1px  #3A76C2;}

   .mytable td{
   padding-top: 10px;
   }
   .mytable .mytable-left{
   text-align: right;
   width : 100px;
   }
  .mytable .mytable-right{
   width : 150px;
   }
</style>
<script type="text/javascript">
//集中获取ModelAndView数据
var path = "${pageContext.request.contextPath}" + "/";
var storageFilter = "${storageFilter}";//定义料仓过滤
//定义jQuery选择器
var systemtips=null;
var toStockType = null;
var toStockGrid = null;
var toStockContentGrid = null;
var toAnaGrid = null;
var inputMoveWeight = null;
var buttonLoad = null;
var fromStockType = null;
var fromStockGrid = null;
var fromStockContentGrid = null;
var fromAnaGrid = null;

//引用通用条件查询网址，设置表格提交的网址
var searchUrl = path + "rm/filterQuery.html";
var omComboUrl = path + "sm/omComboQuery.html";
var moveUrl = path + "rm/MatMoveCtl/loadManual.html";

$(function(){
	initVar();
	initLayout();
	initGrid();
	initComponent();
	loadMove();
});

function initVar(){
	systemtips= $("#systemtips");
	toStockType = $("#toStockType");
	toStockGrid = $("#toStockGrid");
	toStockContentGrid = $("#toStockContentGrid");
	toAnaGrid = $("#toAnaGrid");
	inputMoveWeight = $("#inputMoveWeight");
	buttonLoad = $("#buttonLoad");
	fromStockType = $("#fromStockType");
	fromStockGrid = $("#fromStockGrid");
	fromStockContentGrid = $("#fromStockContentGrid");
	fromAnaGrid = $("#fromAnaGrid");
}

function initLayout(){
	$("#pageLayout").omBorderLayout({
		panels : [{
			id : "pageLayout1",
			region : "north",
			header : false,
			height : 311,
		},{
			id : "pageLayout2",
			region : "center",
			header : false,
		},{
			id : "pageLayout3",
			region : "south",
			header : false,
			height : 311,
		}],
		spacing : 3,
	});
	$("#fromLayout").omBorderLayout({
		panels : [{
			id : "fromLayout1",
			region : "west",
			collapsible : true,
			width : 310,
		},{
			id : "fromLayout2",
			region : "center",
			header : false,
		},{
			id : "fromLayout3",
			region : "east",
			header : false,
			width : 150,
		}],
		spacing : 3,
	});
	$("#toLayout").omBorderLayout({
		panels : [{
			id : "toLayout1",
			region : "west",
			collapsible : true,
			width : 310,
		},{
			id : "toLayout2",
			region : "center",
			header : false,
		},{
			id : "toLayout3",
			region : "east",
			header : false,
			width : 150,
		}],
		spacing : 3,
	});
}

function initComponent(){
	fromStockType.omCombo({
		dataSource : [ {
			text : '合金中间仓',
			value : 'ALLOY'
		}, {
			text : '镍库',
			value : 'NI'
		} ],
		listMaxHeight : 250,
		onValueChange : function(target, newValue, oldValue, event) {
			getStock('U', newValue, fromStockGrid);
		}
	});
	fromStockType.omCombo({
		value : storageFilter,
		disabled : true
	});
	var queryString = "select t.storageLoc";
	if(storageFilter=='MANUAL')
		queryString += " from VSmRmStorage t where t.storageType='M'";
	else if(storageFilter=='WIRE')
		queryString += " from VSmRmStorage t where t.storageType='W'";
	else
		queryString += " from VSmRmStorage t where t.storageType='B'";
	queryString += " group by t.storageLoc order by t.storageLoc";
	var toComboUrl = omComboUrl + "?queryString=" + queryString;
	toStockType.omCombo({
		dataSource : toComboUrl,
		listMaxHeight : 250,
		onValueChange : function(target, newValue, oldValue, event) {
			if(storageFilter=='MANUAL')
				getStock('M', newValue, toStockGrid);
			else if(storageFilter=='WIRE')
				getStock('W', newValue, toStockGrid);
			else
				getStock('B', newValue, toStockGrid);
			
		}
	});
	$("#refreshFrom").click(function() {
		fromStockGrid.omGrid('reload');
	});
	$("#refreshTo").click(function() {
		toStockGrid.omGrid('reload');
	});
}
function getStock(storageType, storageLoc, stockGrid) {
	var group = {
		"op" : "and",
		"rules" : [ {
			op : "equal",
			field : "storageType",
			value : storageType,
			type : "string"
		}, {
			op : "equal",
			field : "storageLoc",
			value : storageLoc,
			type : "string"
		} ]
	};
	//var where = "?domain=VSmRmStorage&rules=" + JSON.stringify(group);
	// 根据网址搜索目的料仓,这里只能用setData方式，否则出现格式异常
	//stockGrid.omGrid('setData', searchUrl + where);
	
	var extraData = {
			"domain" : "VSmRmStorage",
			"rules" : JSON.stringify(group),
			"sortBy": "storageId",
	};
	stockGrid.omGrid({
		dataSource : searchUrl,
		extraData : extraData, 
		method:'POST'
	}, 'reload');

};
function initGrid(){
	fromStockGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:259,
		showIndex:false,
		colModel : [
				{header : '料仓Id', name : 'storageId', align : 'center', width : '100', },
				{header : '料仓名称', name : 'storageName', align : 'center', width : '70', },
				{header : '重量(KG)', name : 'totalWgt', align : 'center', width : '70', },
		            ],
        onRowClick : function(rowIndex, rowData, event) {
        	var group ={
        			op : "and",
        			rules : [{op : 'equal', field : 'storageId', value : rowData['storageId'], type : 'string'}]
        	};
        	var extraData = {
        			"domain" : "SmRmStorageContent",
        			"rules" : JSON.stringify(group),
        	};
        	fromStockContentGrid.omGrid({
        		dataSource : searchUrl,
        		extraData : extraData, 
        		method:'POST'
        	}, 'reload');
        }
	});
	fromStockContentGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:306,
		title:'源料仓数据',
		colModel : [
				{header : '料仓编码', name : 'storageId', align : 'center', width : '60', sort:'serverSide'},
				{header : '计量单号', name : 'measureId', align : 'center', width : '160', sort:'serverSide'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '80', sort:'serverSide'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '120', sort:'serverSide'},
				{header : '重量(KG)', name : 'weight', align : 'center', width : '80', sort:'serverSide'},
				{header : '下料时间', name : 'timeStamp', align : 'center', width : '160', sort:'serverSide'},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
	       	var group ={
	       			op : "and",
	       			rules : [{op : 'equal', field : 'qualityBatch', value : rowData['qualityBatch'], type : 'string'}]
	       	};
	       	var extraData = {
	       			"domain" : "SmRmQualityElement",
	       			"rules" : JSON.stringify(group),
	       	};
	       	fromAnaGrid.omGrid({
	       		dataSource : searchUrl,
	       		extraData : extraData, 
	       		method:'POST'
	       	}, 'reload');
		}
	});
	fromAnaGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:306,
		showIndex:false,
		colModel : [
				{header : '成分名', name : 'storageId', align : 'center', width : '61', sort:'serverSide'},
				{header : '成分值', name : 'storageId', align : 'center', width : '61'},
		            ],
	});
	
	toStockGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:259,
		showIndex:false,
		colModel : [
				{header : '料仓Id', name : 'storageId', align : 'center', width : '100', },
				{header : '料仓名称', name : 'storageName', align : 'center', width : '70', },
				{header : '重量(KG)', name : 'totalWgt', align : 'center', width : '70',},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
	       	var group ={
	       			op : "and",
	       			rules : [{op : 'equal', field : 'storageId', value : rowData['storageId'], type : 'string'}]
	       	};
	       	var extraData = {
	       			"domain" : "SmRmStorageContent",
	       			"rules" : JSON.stringify(group),
	       	};
	       	toStockContentGrid.omGrid({
	       		dataSource : searchUrl,
	       		extraData : extraData, 
	       		method:'POST'
	       	}, 'reload');
		}
	});
	toStockContentGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:306,
		title:'目的料仓数据',
		colModel : [
				{header : '料仓编码', name : 'storageId', align : 'center', width : '60', sort:'serverSide'},
				{header : '计量单号', name : 'measureId', align : 'center', width : '160', sort:'serverSide'},
				{header : '物料编码', name : 'materialId', align : 'center', width : '80', sort:'serverSide'},
				{header : '物料描述', name : 'materialDesc', align : 'center', width : '120', sort:'serverSide'},
				{header : '重量(KG)', name : 'weight', align : 'center', width : '80', sort:'serverSide'},
				{header : '下料时间', name : 'timeStamp', align : 'center', width : '160', sort:'serverSide'},
		            ],
        onRowClick : function(rowIndex, rowData, event) {
	       	var group ={
	       			op : "and",
	       			rules : [{op : 'equal', field : 'qualityBatch', value : rowData['qualityBatch'], type : 'string'}]
	       	};
	       	var extraData = {
	       			"domain" : "SmRmQualityElement",
	       			"rules" : JSON.stringify(group),
	       	};
	       	toAnaGrid.omGrid({
	       		dataSource : searchUrl,
	       		extraData : extraData, 
	       		method:'POST'
	       	}, 'reload');
		}
	});
	toAnaGrid.omGrid({
		limit : 0,
		singleSelect : true,
		height:306,
		showIndex:false,
		colModel : [
				{header : '成分名', name : 'storageId', align : 'center', width : '40', sort:'serverSide'},
				{header : '成分值', name : 'storageId', align : 'center', width : '40'},
		            ],
	});
}

function loadMove(){
	inputMoveWeight.omNumberField({
		allowDecimals : true,
		allowNegative : false,
		decimalPrecision : 1
	});
	buttonLoad.omButton({
		onClick : function(event) {
			onLoadBtn();
		}
	});
	onLoadBtn = function() {
		var fromRows = null;
		var toRows = null;
		try {
			fromRows = fromStockGrid.omGrid('getSelections', true);
			if (fromRows.length != 1) {
				$.omMessageBox.alert({
					content : '请选择低位料仓',
				});
				return false;
			}
		} catch (e) {
			$.omMessageBox.alert({
				content : '请选择低位料仓',
			});
			return false;
		}

		try {
			toRows = toStockGrid.omGrid('getSelections', true);
			if (toRows.length != 1) {
				$.omMessageBox.alert({
					content : '请选择高位料仓',
				});
				return false;
			}
		} catch (e) {
			$.omMessageBox.alert({
				content : '请选择高位料仓',
			});
			return false;
		}

		if (inputMoveWeight.val() == null || inputMoveWeight.val() == '') {
			$.omMessageBox.alert({
				content : '请输入移动重量',
			});
			return false;
		}
		var fromRow = fromRows[0];
		var toRow = toRows[0];
		var weight = inputMoveWeight.val();
		moveMat(fromRow.storageId, toRow.storageId, weight);
	};

	moveMat = function(fromStorageId, toStorageId, weight) {
		var info = {
			"from" : fromStorageId,
			"to" : toStorageId,
			"weight" : weight
		};
		$.post( moveUrl,info, 
				function(data, textStatus)
				{
			     if( data.success )
					{
			    	 $.omMessageBox.alert( { type: "success", title:"成功", content: data.message } );
			    	 
			    	 fromStockContentGrid.omGrid( "reload" );
			    	 toStockContentGrid.omGrid( "reload" );
					}
			     else
			    	 {
						$.omMessageBox.alert( { type: "error", title:"失败", content: data.message } );
			    	 }						
				});
	};
}

</script>

<title></title>
</head>
<body>
 <div id="pageLayout" style="height:685px;">
    	<div id="pageLayout1">
    		<div id="toLayout" style="height:100%">
    			<div id="toLayout1">
    				<label>料仓子类：</label>
    				<input id="toStockType"></input>
    				<button id="refreshTo">刷新</button>							
    				<table id="toStockGrid"></table>		
    			</div>
    			<div id="toLayout2">
    				<table id="toStockContentGrid"></table>
    			</div>
    			<div id="toLayout3">
    				<table id="toAnaGrid"></table>
    			</div>
    		</div>    	
		</div>
    	<div id="pageLayout2" style="padding: 0 10px;">
    		<div style="margin-left:auto;margin-right:auto;margin-top:20px;width:400px;">
    			<label>重量：</label>
    			<input id="inputMoveWeight"></input>
    			<label>KG</label>
    			<button id="buttonLoad" value='高位料仓上料'></button>
			</div>    		
		</div>    	
    	<div id="pageLayout3">
    		<div id="fromLayout" style="height:100%">
    			<div id="fromLayout1">
    				<label>料仓子类：</label>
    				<input id="fromStockType"></input>
    				<button id="refreshFrom">刷新</button>				  			
					<table id="fromStockGrid"></table>			 				
    			</div>
    			<div id="fromLayout2">
    				<table id="fromStockContentGrid"></table>
    			</div>
    			<div id="fromLayout3">
    				<table id="fromAnaGrid"></table>
    			</div>
    		</div>
		</div>
 </div>
 <div id="systemtips" title="系统提示信息"></div> 
</body>
</html>